package io.awen.modules.sign.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.awen.common.utils.PageUtils;
import io.awen.common.utils.Query;
import io.awen.modules.sign.dao.SignCarSpeedingWarningDao;
import io.awen.modules.sign.entity.SignCarEntity;
import io.awen.modules.sign.entity.SignCarSpeedingWarningEntity;
import io.awen.modules.sign.qo.SignCarQo;
import io.awen.modules.sign.service.SignCarService;
import io.awen.modules.sign.service.SignCarSpeedingWarningService;
import io.awen.modules.sys.entity.SysUserEntity;
import io.awen.modules.sys.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 车辆超速时产生一条报警记录
 * (SignCarSpeedingWarning)表服务实现类
 */
@Service("signCarSpeedingWarningService")
public class SignCarSpeedingWarningServiceImpl extends ServiceImpl<SignCarSpeedingWarningDao, SignCarSpeedingWarningEntity> implements SignCarSpeedingWarningService {

    @Autowired
    private SignCarService signCarService;
    @Autowired
    private SysUserService sysUserService;



    /**
     * 当超速的时候自动添加
     */
    @Override
    public void insertWhenSpeeding(SignCarEntity signCar, Double speed, Double lon, Double lat ) {
        if(signCar != null){
            Double limitSpeed = signCar.getLimitSpeed();
            if(speed > limitSpeed){
                SignCarSpeedingWarningEntity entity = new SignCarSpeedingWarningEntity();
                entity.setCarSpeed(speed);
                entity.setLimitSpeed(signCar.getLimitSpeed());
                if (signCar.getLimitSpeed() != null && speed != null) {
                    Double rate = (speed - signCar.getLimitSpeed()) / speed * 100;
                    entity.setOverSpeedRate(rate);
                }
                entity.setSignCarNumberPlate(signCar.getNumberPlate());
                entity.setSignCarId(signCar.getId());
                entity.setLon(lon);
                entity.setLat(lat);
//                signCarSpeedingWarningDao.insert(signCarSpeedingWarningEntity);
                this.save(entity);
            }
        }
    }


    @Override
    public PageUtils queryPage(SignCarQo qo) {
        IPage<SignCarSpeedingWarningEntity> page = this.page(
                new Query<SignCarSpeedingWarningEntity>().getPage(qo),
                new QueryWrapper<SignCarSpeedingWarningEntity>().lambda()
                        .like(StringUtils.isNotBlank(qo.getNumberPlate()), SignCarSpeedingWarningEntity::getSignCarNumberPlate, qo.getNumberPlate())
                        .ge(qo.getStartTime() != null, SignCarSpeedingWarningEntity::getCreateTime, qo.getStartTime())
                        .le(qo.getEndTime() != null, SignCarSpeedingWarningEntity::getCreateTime, qo.getEndTime())
                        .ge(qo.getOverRateMin() != null, SignCarSpeedingWarningEntity::getOverSpeedRate, qo.getOverRateMin())
                        .le(qo.getOverRateMax() != null, SignCarSpeedingWarningEntity::getOverSpeedRate, qo.getOverRateMax())
                        .orderByDesc(SignCarSpeedingWarningEntity::getCreateTime)
        );

        for (SignCarSpeedingWarningEntity entity : page.getRecords()) {
            SignCarEntity car = signCarService.getById(entity.getSignCarId());
            if(car!=null){
                SysUserEntity user = sysUserService.getById(car.getDriverId());
                entity.setUserEntity(user);
            }
            entity.setSignCarEntity(car);
        }

        return new PageUtils(page);
    }

}
